/*--------------------权限模块.菜单管理--ly-----------------*/
/*--------------------（管理员权限）菜单修改功能 menu_edit.html------------------*/

$(document).ready(function() {

	//获取cookie:ueserid & Wtid
	var menuid = $.cookie("menuid");
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	//填充数据
	var searchurl = jurisdictionport + '/ResourceController/selectByPrimaryKey'; //查询
	$.get(searchurl, {
		'resourceid': menuid
	}, function(result) {
		var code = result.code;
		var editlist = result.object;
		if(code == 0) {
			$("#L_name").val(editlist.name); //菜单名
			$("#L_parentid").val(editlist.parentid); //父级菜单
			$("#L_addr").val(editlist.addr); //菜单地址
			$("#L_type").val(editlist.type); //资源类型
			$("#L_imageaddr").val(editlist.imageaddr); //图标元素
			$("#L_description").val(editlist.description); //描述
		} else {
			console.log("网络不佳，稍后再试！")
		}
	});

	//layui
	layui.use(['form', 'layer'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer;

		//自定义验证规则
		form.verify({
			name: function(value) {
				if(value.length < 2) {
					return '昵称至少得5个字符啊';
				}
			}
		});

		//获取表单数据
		$("input").blur(function() {
			//失去焦点事件
			var name = $("#L_name").val(); //菜单名
			var parentid = $("#L_parentid").val(); //父级菜单
			var addr = $("#L_addr").val(); //菜单地址
			var type = $("#L_type").val(); //资源类别
			var isend = $("#L_isend").val(); //资源类别
			var imageaddr = $("#L_imageaddr").val(); //图标元素
			var description = $("#L_description").val(); //描述
			//监听提交
			form.on('submit(add)', function() {
				//递交修改
				editData(name, parentid, addr, type, isend, imageaddr, description);
				return false;
			});
		});

		//修改资源
		var editurl = jurisdictionport+ '/ResourceController/modifyByResourceid'; //修改
		// 获得frame索引
		var index = parent.layer.getFrameIndex(window.name);
		var editData = function(name, parentid, addr, type, isend, imageaddr, description) {
			//修改 edit
			$.get(editurl, {
				resourceid: menuid,
				name: name,
				parentid:parentid,
				addr:addr,
				type:type,
				isend:isend,
				imageaddr:imageaddr,
				description:description,
			}, function(result) {
				var code = result.code;
				if(code == 0) {
					layer.msg('修改成功!', {
						icon: 1,
						time: 1000
					}, function() {
						//关闭当前frame
						parent.layer.close(index);
					});
				} else {
					layer.msg('网络开小差了，请稍后再试!', {
						icon: 2,
						time: 1000
					}, function() {
						//关闭当前frame
//						parent.layer.close(index);
					});
				}
			})
		}

	});
})